package com.zdy.im.user.dao;


import java.util.Map;

import com.sixeco.framework.core.db.ext.Query;
import com.sixeco.framework.core.mybatis.MyBatisBaseCacheDao;
import com.sixeco.framework.util.PageUtils;
import com.zdy.im.user.mapper.ImUserFriendShipMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import com.zdy.im.user.entity.ImUserFriendShip;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;




@Service("imUserFriendShipDao")
public class ImUserFriendShipDao extends MyBatisBaseCacheDao<ImUserFriendShipMapper, ImUserFriendShip> {

    @Autowired
    private ImUserFriendShipMapper  imUserFriendShipMapper;

    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ImUserFriendShip>  iPage =  new Query<ImUserFriendShip>().getPage(params);

        QueryWrapper<ImUserFriendShip> query =  new QueryWrapper<>();

        IPage<ImUserFriendShip> page = this.page(iPage,query );

        return new PageUtils(page);
    }


    @Override
    public void saveData(List<Object> list) {
        List<ImUserFriendShip> datas = new ArrayList<>();
        list.forEach(e -> datas.add(ImUserFriendShip.build().clone(e)));
        this.saveBatch(datas, BATCH_SIZE);
    }

    public ImUserFriendShip selectFriend(long userId,long friendId){
        QueryWrapper<ImUserFriendShip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("friend_id",friendId)
                .eq("status",1);
        //return this.getOne(queryWrapper,false);
        List<ImUserFriendShip> imUserFriendShips = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(imUserFriendShips)){
            return null;
        }
        return imUserFriendShips.get(0);
    }

    /**
     * 查询某个用户，所有好友列表
     * @param userId
     * @return
     */
    public List<ImUserFriendShip> selectListByUserId(long userId){
        QueryWrapper<ImUserFriendShip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("status",1)
                .orderByDesc("sort");
        return this.list(queryWrapper);
    }

    /**
     * 查询某个用户，所有好友列表
     * @param userId
     * @return
     */
    public List<ImUserFriendShip> selectListByUserId(long userId,String name){
        QueryWrapper<ImUserFriendShip> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(name)){
            queryWrapper.like("friend_remarks",name);
        }
        queryWrapper.eq("user_id", userId)
                .eq("status",1)
                .orderByDesc("sort");
        return this.list(queryWrapper);
    }

    /**
     * 查询某个用户,某个组下，所有好友列表
     * @param userId
     * @return
     */
    public List<ImUserFriendShip> selectListByUserIdAndGroupId(long userId,long userGroupId){
        QueryWrapper<ImUserFriendShip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("user_group_id",userGroupId)
                .eq("status",1)
                .orderByAsc("sort");
        return this.list(queryWrapper);
    }

    /**
     * 根据userId,groupId,查询某个用户，某个组的最大排序sort
     * @param userId
     * @return
     */
    public ImUserFriendShip selectOneByUserIdAndGroupId(long userId,long userGroupId){
        QueryWrapper<ImUserFriendShip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("user_group_id",userGroupId)
                .eq("status",1)
                .orderByDesc("sort");
        //return this.getOne(queryWrapper,false);
        List<ImUserFriendShip> imUserFriendShips = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(imUserFriendShips)){
            return null;
        }
        return imUserFriendShips.get(0);
    }

    /**
     * 查询某个组下，所有好友列表
     * @param userGroupId
     * @return
     */
    public List<ImUserFriendShip> selectListByUserGroupId(long userGroupId){
        QueryWrapper<ImUserFriendShip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_group_id", userGroupId)
                .eq("status",1)
                .orderByDesc("sort");
        return this.list(queryWrapper);
    }
}